﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using DIST.LR.ZHJG.Events;
using HSE.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using HSE.Common;

namespace HSE
{
    public partial class Whyl : UserControl
    {

        public WhylViewModel viewmodel;
        IModuleManager  _moduleManager;
        IUnityContainer _container;
        IRegionManager _regionManager;
        const string ModuleFlag = "Whyl";

        Storyboard moduleInSb;
        Storyboard moduleOutSb;

        ToolBarNaviInfo _currentNavi;

        public Whyl(IModuleManager moduleManager, IUnityContainer container, IRegionManager regionManager)
        {
            InitializeComponent();

            moduleInSb = (Storyboard)this.Resources["ModuleIn"];
            moduleOutSb = (Storyboard)this.Resources["ModuleOut"];
            moduleOutSb.Completed += new EventHandler(moduleOutSb_Completed);


            _moduleManager = moduleManager;
            _container = container;
            _regionManager = regionManager;
            

            viewmodel = new WhylViewModel();
            this.DataContext = viewmodel;
            this.navi.Binding(ModuleFlag, viewmodel.Functions);
            EventAggregatorRepository.EventAggregator
               .GetEvent<NaviClickEvent>()
               .Subscribe(NaviClickHandler);
        }

        public void NaviClickHandler(NaviClickInfo info)
        {
            if (ModuleFlag == info.Owner)
            {
                _currentNavi = info.NaviInfo;
                moduleOutSb.Begin();      
            }
              
        }

        void moduleOutSb_Completed(object sender, EventArgs e)
        {
            moduleInSb.Begin();
            viewmodel.SelectModule = _currentNavi.Name;
            _moduleManager.LoadModule(_currentNavi.ModuleName);
            _regionManager.RequestNavigate("Whyl_ModuleContainer", new Uri(_currentNavi.ViewName, UriKind.Relative));
        }

        
    }
}
